<?php
class Poll_Widget_BrowseController extends Engine_Content_Widget_Abstract
{
    public function indexAction() {
        $this->view->isAjax = $isAjax = $this->_getParam('isAjax');
        if ($isAjax) {
            $this->getElement()->removeDecorator('Title');
            $this->getElement()->removeDecorator('Container');
        }
        $this->view->viewer = $viewer   = Engine_Api::_()->user()->getViewer();
        $privacy  = $this->_getParam('privacy', 'all');         
        $order = $this->_getParam('order', 'poll_id DESC');         
        $request = Zend_Controller_Front::getInstance()->getRequest();
        if ($request->isPost()) {                                                                   
            $form = new Poll_Form_Search();
            if ($form->isValid($request->getPost())) {
                $values = $form->getValues();
                $privacy = $values['privacy'];
                if (!empty($values['search'])) {
                    $this->view->search = $params['search'] = $values['search'];
                }
                $order = $values['order'];
            }
        }
        $params['order'] = $order;
        $params['privacy'] = $privacy;
        $params['viewer'] = $viewer->getIdentity();
        
        $this->view->privacy = $privacy;
        $this->view->order = $order;
        $this->view->paginator = $paginator = Engine_Api::_()->poll()->pollPaginator($params);
        $paginator->setItemCountPerPage(15);
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
    }
}
